package com.lmq.Utils
import org.apache.spark.util.AccumulatorV2
import scala.collection.mutable

/**
 * User defined accumulatorV2
 */
class SetAccumulatorV2 extends AccumulatorV2[String, mutable.HashSet[String]] {
  private var strings: mutable.HashSet[String] = mutable.HashSet[String]()
  override def isZero: Boolean = strings.isEmpty

  override def copy(): AccumulatorV2[String, mutable.HashSet[String]] = {
    val v = new SetAccumulatorV2()
    v.strings = this.strings
    v
  }

  override def reset(): Unit = strings.clear()

  override def add(v: String): Unit = strings += v

  override def merge(other: AccumulatorV2[String, mutable.HashSet[String]]): Unit = strings ++= other.value

  override def value: mutable.HashSet[String] = strings
}
